import coin.exchange.base.kr_rest.native_public_client_base as npubcb


class GdaxNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.pro.coinbase.com")
    self._valid_kline_period = [60, 300, 900, 3600, 21600, 86400]

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
        'Invalid kline period!'

  # GDAX's "product_id" is the same as our "native_symbol"
  def get_product_ticker(self, product_id):
    return self._get_as_json("products/%s/ticker" % product_id)

  def get_products_book(self, product_id, level=None):
    if level is not None:
      assert level in [1, 2, 3]
      return self._get_as_json("products/%s/book?level=%d" % (product_id, level))
    else:
      return self._get_as_json("products/%s/book" % (product_id))

  def get_history_kline(self, product_id, kline_period, start_time, end_time):
    self.check_kline_period(kline_period)
    url = "products/%s/candles" % product_id
    params = {
        'granularity': kline_period, 'start': start_time.isoformat(), 'end': end_time.isoformat()
    }
    return self._get_as_json(url, params=params)

  def queryAll(self):
    currency = "BTC-USD"
    print(self.get_products_book(currency))


if __name__ == "__main__":
  client = GdaxNativePublicClient()
  client.queryAll()
  print(client.get_kline_history('BTC-USD', 60))
